package com.user.query.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.user.query.dao.AdminAreaMapper;
import com.user.query.model.AdminArea;
import com.user.query.model.User;
import com.user.query.service.AdminAreaService;
import com.user.query.utils.Page;


@Service
public class AdminAreaServiceImpl implements AdminAreaService {
	
	@Resource
	private AdminAreaMapper adminAreaMapper;

	@Override
	public AdminArea findByAreaNo(String areaNo) {
		
		return adminAreaMapper.findByAreaNo(areaNo);
	}

	@Override
	public void insert(AdminArea adminArea) {
		adminAreaMapper.insert(adminArea);
	}

	@Override
	public void updateByPrimaryKeySelective(AdminArea adminArea) {
		adminAreaMapper.updateByPrimaryKeySelective(adminArea);
	}

	@Override
	public AdminArea selectByPrimaryKey(String areaId) {
		
		return adminAreaMapper.selectByPrimaryKey(areaId);
	}

	@Override
	public List<AdminArea> findAllByPage(String keyWord, Page page) {
		
		return adminAreaMapper.findAllByPage(keyWord,page);
	}

	@Override
	public List<AdminArea> findAllByLevel(Integer level) {
		
		return adminAreaMapper.findAllByLevel(level);
	}
	@Override
	public List<AdminArea> findAllByLevel2(Integer level) {
		
		return adminAreaMapper.findAllByLevel2(level);
	}

	@Override
	public int countAllByPage(String keyWord) {
		return adminAreaMapper.countAllByPage(keyWord);
	}

	@Override
	public List<AdminArea> queryByLoginUser(User loginUser) {
		String userArea;
		int userAreaIndex, level = 4;
		if(StringUtils.isNotEmpty(loginUser.getVillage())) {
			userArea = loginUser.getVillage();
			userAreaIndex = 12;
		}else if(StringUtils.isNotEmpty(loginUser.getTown())) {
			userArea = loginUser.getTown();
			userAreaIndex = 9;
		}else if(StringUtils.isNotEmpty(loginUser.getCounty())) {
			userArea = loginUser.getCounty();
			userAreaIndex = 6;
		}else if(StringUtils.isNotEmpty(loginUser.getCity())) {
			userArea = loginUser.getCity();
			userAreaIndex = 4;
		}else if(StringUtils.isNotEmpty(loginUser.getProvince())) {
			userArea = loginUser.getProvince();
			userAreaIndex = 2;
			level = 3;
		}else { 
			userArea = "";
			userAreaIndex = 0;
			level = 3;
		}
		
		
		userArea = "".equals(userArea)?userArea:userArea.substring(0, userAreaIndex);
		return adminAreaMapper.likeByAreaNo(userArea, level);
	}

	@Override
	public List<AdminArea> findAllByToNo(String topNo) {
		
		return adminAreaMapper.findAllByToNo(topNo);
	}

	@Override
	public List<AdminArea> findAll() {
		return adminAreaMapper.findAll();
	}
	
	@Override
	public List<AdminArea> findToExam(String city) {
		return adminAreaMapper.findToExam(city);
	}

	@Override
	public List<AdminArea> findCityByAreaNo(String AreaNo) {
		// TODO Auto-generated method stub
		return adminAreaMapper.findCityByAreaNo(AreaNo);
	}
}
